home *** CD-ROM | disk | FTP | other *** search
/ User's Choice Windows CD / User's Choice Windows CD (CMS Software)(1993).iso / windows5 / xwinc100.zip / CONTRIB-.00 / CONTRIB- / contrib / examples / Xaw / xmenu2.c < prev    next >
C/C++ Source or Header  |  1991-01-22  |  6KB  |  187 lines

  1. /*
  2.  * A more complicated Simple Menu Widget example.
  3.  * 
  4.  * November 30, 1989 - Chris D. Peterson
  5.  */
  6.  
  7. /*
  8.  * $XConsortium: xmenu2.c,v 1.10 91/01/22 19:50:01 gildea Exp $
  9.  *
  10.  * Copyright 1989 Massachusetts Institute of Technology
  11.  *
  12.  * Permission to use, copy, modify, distribute, and sell this software and its
  13.  * documentation for any purpose is hereby granted without fee, provided that
  14.  * the above copyright notice appear in all copies and that both that
  15.  * copyright notice and this permission notice appear in supporting
  16.  * documentation, and that the name of M.I.T. not be used in advertising or
  17.  * publicity pertaining to distribution of the software without specific,
  18.  * written prior permission.  M.I.T. makes no representations about the
  19.  * suitability of this software for any purpose.  It is provided "as is"
  20.  * without express or implied warranty.
  21.  *
  22.  * M.I.T. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
  23.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL M.I.T.
  24.  * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  25.  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  26.  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN 
  27.  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  28.  */
  29.  
  30.  
  31. #include <stdio.h>
  32.  
  33. #include <X11/Intrinsic.h>
  34. #include <X11/StringDefs.h>
  35. #include <X11/bitmaps/xlogo16>
  36.  
  37. #include <X11/Xaw/MenuButton.h>
  38. #include <X11/Xaw/SimpleMenu.h>
  39. #include <X11/Xaw/Sme.h>
  40. #include <X11/Xaw/SmeBSB.h>
  41. #include <X11/Xaw/SmeLine.h>
  42.  
  43. #include <X11/Xaw/Cardinals.h>
  44.  
  45. static void MenuSelect(), Syntax();
  46.  
  47. String fallback_resources[] = { 
  48. #ifdef COLOR_DISPLAY
  49.     "*SimpleMenu*foreground:        SteelBlue",
  50.     "*SimpleMenu*menuLabel.foreground:    Gold",
  51.     "*SimpleMenu*line.foreground:    Grey",
  52. #endif /* COLOR_DISPLAY */
  53.     "*menuButton.label:                Click here for menu",
  54.     "*SimpleMenu*menuLabel.vertSpace:    100",
  55.     "*SimpleMenu*menuLabel.leftMargin:    70",
  56.     "*SimpleMenu.label:         Xmenu2 Menu",
  57.     "*SimpleMenu*quit*label:            Select this item to quit the menu demo.",
  58.     "*SimpleMenu*RowHeight:        16",
  59.     "*SimpleMenu*item7*sensitive:     off",
  60.     "*SimpleMenu*HorizontalMargins:     30",
  61.     "*menuLabel*font: -*-courier-medium-r-normal--34-*-100-100-*-*-iso8859-1",
  62.     "*item1*font: -*-courier-medium-r-normal--11-*-100-100-*-*-iso8859-1",
  63.     "*item2*font: -*-courier-bold-r-normal--11-*-100-100-*-*-iso8859-1",
  64.     "*item3*font: -*-courier-medium-r-normal--14-*-100-100-*-*-iso8859-1",
  65.     "*item4*font: -*-courier-bold-r-normal--14-*-100-100-*-*-iso8859-1",
  66.     "*item5*font: -*-courier-medium-r-normal--17-*-100-100-*-*-iso8859-1",
  67.     "*item6*font: -*-courier-bold-r-normal--17-*-100-100-*-*-iso8859-1",
  68.     "*item7*font: -*-courier-medium-r-normal--20-*-100-100-*-*-iso8859-1",
  69.     "*item8*font: -*-courier-bold-r-normal--20-*-100-100-*-*-iso8859-1",
  70.     NULL,
  71. };
  72.  
  73. #define NUM_MENU_ITEMS 12
  74.  
  75. static char * menu_entry_names[] = {
  76.   "quit", 
  77.   "item1", "item2", "item3", "item4", "item5", "item6", "item7", "item8",
  78.   "menu1", "menu2", "menu3",
  79. };
  80.  
  81. static Boolean status[NUM_MENU_ITEMS];
  82. static Pixmap mark;
  83.  
  84. void
  85. main(argc, argv)
  86. char ** argv;
  87. int argc;
  88. {
  89.     Widget top, menu, button, entry;
  90.     XtAppContext app_con;
  91.     int i;
  92.     top = XtAppInitialize(&app_con, "Xmenu2", NULL, ZERO,
  93.               &argc, argv, fallback_resources,
  94.               NULL, ZERO);
  95.     
  96.     if (argc != 1)        
  97.     Syntax(app_con, argv[0]);
  98.     
  99.     button = XtCreateManagedWidget("menuButton", menuButtonWidgetClass, top,
  100.                    NULL, ZERO);
  101.     
  102.     menu = XtCreatePopupShell("menu", simpleMenuWidgetClass, button, 
  103.                   NULL, ZERO);
  104.   
  105.     for (i = 0; i < NUM_MENU_ITEMS ; i++) {
  106.     char * item = menu_entry_names[i];
  107.     
  108.     entry = XtCreateManagedWidget(item, smeBSBObjectClass, menu, 
  109.                       NULL, ZERO);
  110.     XtAddCallback(entry, XtNcallback, MenuSelect, (XtPointer) i);
  111.  
  112.     /* 
  113.      * Put a couple of line seperators in at the apropriate place. 
  114.      */
  115.  
  116.     if (i == 4) 
  117.         entry = XtCreateManagedWidget("line", smeLineObjectClass, menu,
  118.                       NULL, ZERO);
  119.     if (i == 8) 
  120.         entry = XtCreateManagedWidget("blank", smeObjectClass, menu,
  121.                       NULL, ZERO);
  122.     }
  123.  
  124.     /*
  125.      * Create the bitmap for marking selected items. 
  126.      */
  127.  
  128.     mark = XCreateBitmapFromData(XtDisplay(top),
  129.                  RootWindowOfScreen(XtScreen(top)),
  130.                  (char *)xlogo16_bits,
  131.                  xlogo16_width, xlogo16_height);
  132.  
  133.     XtRealizeWidget(top);
  134.     XtAppMainLoop(app_con);
  135. }
  136.  
  137. /*    Function Name: MenuSelect
  138.  *    Description: called whenever a menu item is selected.
  139.  *    Arguments: w - the menu item that was selected.
  140.  *                 number - the number of the menu item selected.
  141.  *                 garbage - *** unused ***.
  142.  *    Returns: 
  143.  */
  144.  
  145. /* ARGSUSED */
  146. static void
  147. MenuSelect(w, number, garbage)
  148. Widget w;
  149. XtPointer number, garbage;
  150. {
  151.     Arg arglist[1];
  152.     Cardinal num_args = 0;
  153.     int num = (int) number;
  154.     
  155.     printf("Menu item `%s' has been selected.\n", XtName(w));
  156.     
  157.     if (num == 0) {            /* quit selected. */
  158.     XtDestroyApplicationContext(XtWidgetToApplicationContext(w));
  159.     exit(0);
  160.     }
  161.     if (status[num]) 
  162.     XtSetArg(arglist[num_args], XtNleftBitmap, None); 
  163.     else 
  164.     XtSetArg(arglist[num_args], XtNleftBitmap, mark);
  165.     num_args++;
  166.     XtSetValues(w, arglist, num_args);
  167.     
  168.     status[num] = !status[num];
  169. }
  170.  
  171. /*    Function Name: Syntax
  172.  *    Description: Prints a the calling syntax for this function to stdout.
  173.  *    Arguments: app_con - the application context.
  174.  *                 call - the name of the application.
  175.  *    Returns: none - exits tho.
  176.  */
  177.  
  178. static void 
  179. Syntax(app_con, call)
  180. XtAppContext app_con;
  181. char *call;
  182. {
  183.     XtDestroyApplicationContext(app_con);
  184.     fprintf( stderr, "Usage: %s [-label <label name>]\n", call);
  185.     exit(1);
  186. }
  187.